/*
时间戳工具
可用户性能调优时，测量函数运行消耗的时间

 */

package toolbox

import (
	"time"
)



var _tsGroupResult [][]*Timestamp
/*
测量函数运行时间，代码使用例子
func (t *UserService) CheckToken(ctx *fasthttp.RequestCtx, passToken string, timestamp string) (user *LoginUser,  err error) {
	ctxExt := lego.RequestCtxExtent{ctx}
	starttime := toolbox.InsertStartTime(lego.GetTsGroupInContext(ctxExt), "UserApiCtrl::UserProfile")
	defer toolbox.InsertEndTime(starttime)
}


func (t *UserService) CheckToken(ctxExt *lego.RequestCtxExtent, passToken string, timestamp string) (user *LoginUser,  err error) {
	starttime := toolbox.InsertStartTime(lego.GetTsGroupInContext(ctxExt), "UserApiCtrl::UserProfile")
	defer toolbox.InsertEndTime(starttime)
}

*/
func InsertStartTime(tsGroup *[]*Timestamp, name string) *Timestamp {
	ts := NewTimestamp(name)
	*tsGroup = append(*tsGroup, ts)
	return ts
}

func InsertEndTime(ts *Timestamp) {
	ts.SetEndTimestamp()
	//ts.Duration = //ts.endtime - time.starttime
	ts.Duration = toS(time.Since(ts.Starttime))
	return
}




type Timestamp struct {
	Name string `json:"name"`
	Starttime time.Time `json:"starttime"`
	Endtime time.Time `json:"endtime"`
	Duration string `json:"duration"`
}



func NewTimestamp(name string) (*Timestamp){
    ts := &Timestamp{}
    ts.Name = name
    ts.Starttime = time.Now()
    return ts
}

func (t *Timestamp) SetEndTimestamp() {
	t.Endtime = time.Now()
}

func NewTimestampGroup() []*Timestamp {
	tsGroup := make([]*Timestamp, 0, 10)
	return tsGroup
}

func AppendToTimestampResult(tsGroup *[]*Timestamp) {
    _tsGroupResult = append(_tsGroupResult, *tsGroup)
}

func GetTimestampResult() [][]*Timestamp{
	return _tsGroupResult
}


